<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('relation:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="关系名称">
                <a-input v-model="queryParam.chName" allow-clear placeholder="请输入关系名称" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="英文名">
                <a-input v-model="queryParam.enName" allow-clear placeholder="请输入英文名" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="特征">
                  <a-select style="width: 100%" v-model="queryParam.feature" placeholder="请选择特征">
                    <a-select-option
                      v-for="(item,index) in featureData"
                      :key="index"
                      :value="item.code">{{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('relation:add')">
          <a-button type="primary" v-if="hasPerm('relation:add')" icon="plus" @click="$refs.addForm.add()">新增关系
          </a-button>
        </template>
        <span slot="featurescopedSlots" slot-scope="text">
          {{ 'relation_feature' | dictType(text) }}
        </span>
        <span slot="typescopedSlots" slot-scope="text">
          {{ 'relation_type' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('relation:edit')" @click="$refs.qualifierIndex.index(record)">限定词</a>
          <a-divider type="vertical" v-if="hasPerm('relation:edit') & hasPerm('relation:delete')" />
          <a v-if="hasPerm('relation:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('relation:edit') & hasPerm('relation:delete')" />
          <a-popconfirm
            v-if="hasPerm('relation:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => relationDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
      <qualifier-index ref="qualifierIndex" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { relationPage, relationDelete } from '@/api/modular/main/knowl/relation/relationManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
import qualifierIndex from './qualifier/index'

export default {
  components: {
    qualifierIndex,
    STable,
    addForm,
    editForm
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      labelCustomStyle: {
        style: {
          'white-space': 'nowrap',
          'overflow': 'hidden',
          'text-overflow': 'ellipsis'
        }
      },
      // 表头
      columns: [
        {
          title: '关系名称',
          align: 'center',
          dataIndex: 'chName',
          width: '100px',
          customCell: (record, rowIndex) => {
            return this.labelCustomStyle
          }
        },
        {
          title: '英文名',
          align: 'center',
          dataIndex: 'enName',
          width: '100px',
          customCell: (record, rowIndex) => {
            return this.labelCustomStyle
          }
        },
        {
          title: '特征',
          align: 'center',
          dataIndex: 'feature',
          scopedSlots: { customRender: 'featurescopedSlots' },
          width: '120px',
          customCell: (record, rowIndex) => {
            return this.labelCustomStyle
          }
        },
        {
          title: '描述',
          align: 'center',
          dataIndex: 'describes',
          width: '160px',
          customCell: (record, rowIndex) => {
            return this.labelCustomStyle
          }
        },
        {
          title: '概念A',
          align: 'center',
          dataIndex: 'schemaFromName',
          width: '150px',
          customCell: (record, rowIndex) => {
            return this.labelCustomStyle
          }
        },
        {
          title: '概念B',
          align: 'center',
          dataIndex: 'schemaToName',
          width: '150px',
          customCell: (record, rowIndex) => {
            return this.labelCustomStyle
          }
        },
        {
          title: '方向',
          align: 'center',
          dataIndex: 'direct',
          width: '100px',
          customRender: function(t, r, index) {
            if (r.direct === 1) {
              return 'A->B'
            } else if (r.direct === 2) {
              return 'B->A'
            } else {
              return 'A<->B'
            }
          }
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return relationPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      featureData: [],
      typeData: [],
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    if (this.hasPerm('relation:edit') || this.hasPerm('relation:delete')) {
      this.columns.push({
        title: '操作',
        width: '250px',
        align: 'center',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    const featureOption = this.$options
    this.featureData = featureOption.filters['dictData']('relation_feature')
    const typeOption = this.$options
    this.typeData = typeOption.filters['dictData']('relation_type')
  },
  methods: {
    relationDelete(record) {
      relationDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

.ant-tabs-tab {
  font-weight: 700;
  font-size: 15px;
}

.ant-tabs-nav .ant-tabs-tab-active {
  font-weight: 700;
  font-size: 15px;
}

table {
  table-layout: fixed;
}

button {
  margin-right: 8px;
}
</style>
